/**
 * http://localhost:8083/proj
 * 		取得项目根路径
 */
function getRootPath() {
	// 获取当前网址，如： http://localhost:8083/proj/meun.jsp
	var curWwwPath = window.document.location.href;
	// 获取主机地址之后的目录，如： proj/meun.jsp
	var pathName = window.document.location.pathname;
	var pos = curWwwPath.indexOf(pathName);
	// 获取主机地址，如： http://localhost:8083
	var localhostPath = curWwwPath.substring(0, pos);
	// 获取带"/"的项目名，如：/proj
	var projectName = pathName
			.substring(0, pathName.substr(1).indexOf('/') + 1);
	return (localhostPath + projectName);
}

/**
 * 查询商品分类列表数据
 */
$(function(){
	var currentPage = 1;
	ptlist(currentPage);
	$("#users").change(function(){
		var userid = $(this).val();
		$("#productOderform :input[name=delivery]").val("");
		$("#productOderform :input[name=userName]").val("");
		$("#productOderform :input[name=phone]").val("");
		$("#productOderform :input[name=address]").val("");
		$.ajax({
			type : 'post',
			url : getRootPath() + "/ConvertIn/getDevInfo.do",
			data : {
				userid : userid
			},
			async : 'true',
			dataType : 'json',
			success : function(data){
				dev = eval(data.devInfo);
				$("#productOderform :input[name=delivery]").val(dev.delivery);
				$("#productOderform :input[name=userName]").val(dev.user_name);
				$("#productOderform :input[name=phone]").val(dev.phone);
				$("#productOderform :input[name=address]").val(dev.address);
			}
		});
	});
});

function ptlist(currentPage){
	$.ajax({
		type : 'post',
		url : getRootPath() + "/ConvertIn/ConvertIndentOrder.do",
		data : {
			"memberUnit" : 'member001',
			"currentPage" : currentPage
		},
		async : 'true',
		dataType : 'json',
		success : function(data){
			var rec = data.page;
			// alert(rec.list[1].productTypeName);
			var html = "";
			for(i = 0; i < rec.list.length; i++){
				html += "<tr>";
				html += "<td style='padding-top: 18px;'><input type='checkbox' id='ck-all' name='ck-all' value="+rec.list[i].id+" ></td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].id+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].covertId+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].productName+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].placeTime+"</td>";
				html += "<td style='padding-top: 18px;'>"+statusFormat(rec.list[i].orderStatus)+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].remarks+"</td>";
				html += "</tr>";
			}
			$("#data_list").html(html);
			//鼠标经过td的时候就显示td内容
		    jQuery(function(){
		        //使用class选择器;例如:list对象->tbody->td对象.
		           $("#data_list td").each(function(i){
		                       //给td设置title属性,并且设置td的完整值.给title属性.
		                       $(this).attr("title",$(this).text());

		             });
		       });
			var pager = data.page;
			$(".pag_in").html(
					G.getPager(pager.totalRecords,
							pager.currentPage, 10, 10,
							'ptlist'));
		}
		
	});
}

function statusFormat(status){
	if(status=='1'){
		return '申请';
	}else if(status=='2'){
		return '审核';
	}else if(status=='3'){
		return '取消';
	}else{
		return '发货';
	}
}

function cancelOrder(){
	var chk_value = [];
	var len = $('#data_list :input[name="ck-all"]').length;
	var id;
	var num = 0;
	$('input[name="ck-all"]:checked').each(function() {
		num ++;
		id = $(this).val();
	});
	if(num > 1){
		alert("每次只能选择一条订单取消@@@");
	} else {
		$.ajax({
			type : 'post',
			url : getRootPath() + "/creditsR/cancelOrder.do",
			data : {
				reason:"就TM不想换了",
				id:id
			},
			async : 'false',
			dataType : 'json',
			success : function(data){
				if (data.resultMsg != 1) {
					layer.msg('失败',{icon: 2});
				} else{
					layer.msg('成功',{icon:1});
					$("#myModal_1").modal("hide");
					ptlist(1);
				}
			}
		});
	}
}

function insert(){
	$.ajax({
		type : 'post',
		url : getRootPath() + "/ConvertIn/insertPRORDER.do",
		data : $('#productOderform').serialize(),
		async : 'false',
		dataType : 'json',
		success : function(data){
			if (data.resultMsg != 1) {
				layer.msg('失败',{icon: 2});
			} else{
				layer.msg('成功',{icon:1});
				$("#myModal_1").modal("hide");
				ptlist(1);
			}
		}
	});
}

function search(currentPage){
	var userLevelId = $("#searchForm select[name=userLevelId]").val();
	var stauts = $("#searchForm select[name=status]").val();
	var keyWord = $("#searchForm input[name=keyWord]").val();
	$.ajax({
		type : 'post',
		url : getRootPath() + "/creditsT/ctList.do",
		data : {
			"currentPage" : currentPage,
			"userLevelId" : userLevelId,
			"stauts" : status,
			"keyWord" : keyWord
		},
		async : 'true',
		dataType : 'json',
		success : function(data){
			var rec = data.page;
			// alert(rec.list[1].productTypeName);
			var html = "";
			for(i = 0; i < rec.list.length; i++){
				html += "<tr>";
				html += "<td style='padding-top: 18px;'><input type='checkbox' id='ck-all' name='ck-all' value="+rec.list[i].id+" ></td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].id+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].covertId+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].productName+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].sum+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].isStick+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].placeTime+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].status+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].remarks+"</td>";
				html += "</tr>";
			}
			$("#data_list").html(html);
			var pager = data.page;
			$(".pag_in").html(
					G.getPager(pager.totalRecords,
							pager.currentPage, 10, 10,
							'search'));
		}
	});
}

function getPRO(){
	var chk_value = [];
	var len = $('#data_list :input[name="ck-all"]').length;
	var id;
	var num = 0;
	$('input[name="ck-all"]:checked').each(function() {
		num ++;
		id = $(this).val();
	});
	if(num > 1){
		alert("每次只能选择一条记录修改@@@");
	} else {
		getPRO_record(id);
	}
}

function delPRO(){
	var chk_value = [];
	var len = $('#data_list :input[name="ck-all"]').length;
	var ids;
	$('input[name="ck-all"]:checked').each(function() {
		ids += $(this).val()+",";
	});
	delPRO_record(ids);
}

function verify(){
	var chk_value = [];
	var len = $('#data_list :input[name="ck-all"]').length;
	var ids;
	$('input[name="ck-all"]:checked').each(function() {
		ids += $(this).val()+",";
	});
	layer.confirm('是否确认审核？', {
		icon:3,
	    btn: ['审核', '取消'],
	    yes: function(index, layero){
			$.ajax({
				type : 'post',
				url : getRootPath() + "/ConvertIn/verify.do",
				data:{
					"ids":ids
				},
				dataType : 'json',
				success : function(data){
					if (data.resultMsg != 1) {
						layer.msg('失败',{icon: 2});
					} else{
						layer.msg('成功',{icon:1});
						ptlist(1);
					}
				}
			});
	    }, 
		no:function(index){
		    layer.close(index);
		}
	});
}

function delPRO_record(ids){
	$.ajax({
		type : 'post',
		url : getRootPath() + "/ConvertIn/delProOrder.do",
		data:{
			"ids":ids
		},
		dataType : 'json',
		success : function(data){
			if (data.resultMsg != 1) {
				layer.msg('失败',{icon: 2});
			} else{
				layer.msg('成功',{icon:1});
				ptlist(1);
			}
		}
	});
}

function getPRO_record(id){
	$("#users").html("");
	$("#productName").html("");
	$.ajax({
		type : 'post',
		url : getRootPath() + "/ConvertIn/getProOrder.do",
		data : {
			"id" : id
		},
		/*async : 'true',*/
		dataType : 'json',
		success : function(data){
			rec = eval(data.record);
			pro = eval(data.products);
			users = eval(data.users);
			$("#productOderform :input[name=covertId]").val(rec.covert_id);
			$("#productOderform :input[name=sum]").val(rec.sum);
			$("#productOderform :input[name=delivery]").val(rec.delivery);
			$("#productOderform :input[name=remarks]").val(rec.remarks);
			$("#productOderform :input[name=userName]").val(rec.user_name);
			$("#productOderform :input[name=phone]").val(rec.phone);
			$("#productOderform :input[name=address]").val(rec.address);
			$("#productOderform :input[name=placeTime]").val(FormatDateTimestamp(rec.place_time));
			// $("#userLevelId option[value='"+rec.userLevelId+"']").attr("select","selected");
			var userl = rec.is_stick;
			var userls = document.getElementById("isStick"); 
			 var ops = userls.options;
			 for(var i=0;i<ops.length; i++){
				 var userlValue = ops[i].value; 
				 if(userlValue == userl)   //这里是你要选的值  
	                {  
					 $("#isStick").val(userl).trigger("change"); 
	                }  
			 }
			 $.each(pro,function(){
     			$("<option/>").val(this.product_id).
     			 attr("selected",rec.product_id==this.product_id)
     			.html(this.product_name).appendTo("#productName");
     		});
			 
			 $.each(users,function(){
				 $("<option/>").val(this.user_id).
				 attr("selected",rec.CIuser_id==this.user_id)
				 .html(this.real_name).appendTo("#users");
			 });
			 var html = "<tr><td style='padding-top: 18px;'>"+orderFormat(rec.order_status)+"</td>" +
			 		"<td style='padding-top: 18px;'>"+FormatDateTimestamp(rec.update_dt)+"</td>" +
			 		"<td style='padding-top: 18px;'>"+rec.orderRemarks+"</td></tr>";
			$("#list_in").html(html);
		}
	});
}

// jquery获取复选框值
function jqchk() {
	var chk_value = [];
	var len = $('#data_list :input[name="ck-all"]').length;
	var creditsTypeId;
	var num = 0;
	$('input[name="ck-all"]:checked').each(function() {
		num ++;
		creditsTypeId = $(this).val();
	});
	if(num > 1){
		alert("每次只能选择一条记录修改@@@");
	}
	// alert(chk_value.length == 0 ? '你还没有选择任何内容！' : chk_value);
} 

function FormatDateTimestamp(timestamp){ 
	var date=new Date(timestamp);
	return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds(); 
}

function orderFormat(status){
	if(status=="1"){
		return "申请";
	}else if(status == "2"){
		return "审核";
	}else if(status == "3"){
		return "取消";
	}else if(status == "4"){
		return "发货";
	}
}

function openInsert(){
	$("#productName").html("");
	$("#users").html("");
	$.ajax({
		type : 'post',
		url : getRootPath() + "/ConvertIn/openWindows.do",
		/*async : 'true',*/
		dataType : 'json',
		success : function(data){
			$("#productOderform :input").val("");
			$("#list_in").html("");
			pro = eval(data.products);
			users = eval(data.users);
			 $.each(pro,function(){
     		$("<option/>").val(this.product_id)
     			.html(this.product_name).appendTo("#productName");
     		});
			 $.each(users,function(){
				 $("<option/>").val(this.user_id)
				 .html(this.real_name).appendTo("#users");
			});
		}
	});
	
}

function openEdit(){
	
}